<template>
  <el-row>
    <el-col :span="24">
      <el-card >
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6">
              <el-form-item prop="name">
                <el-input v-model="search.name" placeholder="搜索仓库"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="12">
                    <el-button @click="resetFields('searchForm')" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/ch-warehouse-manager/getWarehouseList"
        :tabs="tabs"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="addWarehouse">新增仓库</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="200px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="stopOrStart(scope.row)"
              >
                {{ scope.row.state ? '停用' : '启用' }}
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="update(scope.row)"
              >
                修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="setting(scope.row)"
              >
                设置库位
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="details(scope.row)"
              >
                详情
              </el-button>
              <el-button
                v-if="scope.row.state === 0"
                size="mini"
                type="text"
                @click="deletes(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
   <AddWarehouse ref="AddWarehouse" @refresh="refresh" :warehouseData="warehouseData"></AddWarehouse>
   <DetailsDialog ref="DetailsDialog" @showUpdate="showUpdate" :warehouseData="warehouseData"></DetailsDialog>
  </el-row>
</template>

<script>
import {copy, confirm, alert} from '@/utils'
import {unixTimestampFilter} from '@/filters'
import AddWarehouse from '@/views/oms/warehouse/configuration/AddWarehouse'
import DetailsDialog from '@/views/oms/warehouse/configuration/DetailsDialog'
export default {
  name: 'warehouseConfigurationIndex',
  components: {
    AddWarehouse,
    DetailsDialog
  },
  data() {
    return {
      search: {
        name: ''
      },
      totalItem: 0,
      params: {
        pageSize: 20,
        pageNum: 1
      },
      searchOpen: false,
      searchLoading: false,
      warehouseData: {},
      tabs: [],
      columns: [
        {
          label: '仓库编号',
          data: 'code'
        },
        {
          label: '仓库名称',
          data: 'name'
        },
        {
          label: '仓库类型',
          data: 'warehouseType',
          render: (data, full) => {
            switch (data) {
              case 1:
                return '收货仓'
              case 2:
                return '落货仓'
              default:
                return '未知'
            }
          }
        },
        {label: '所属公司编号', data: 'companyCode'},
        {label: '所属公司名称', data: 'companyName'},
        {
          label: '仓库地址',
          data: 'houseAddress',
          width: '200px',
          render: (data, full) => {
            return full.houseProvince + full.houseCity + full.houseDistrict + full.houseAddress
          }
        },
        {
          label: '仓库覆盖区域',
          data: 'coverageArea',
          width: '300px'
        },
        {label: '仓库面积', data: 'houseArea'},
        {
          label: '仓管人员',
          data: 'houseAdminUser'
        },
        {
          label: '创建人',
          data: 'founder'
        },
        {
          label: '创建时间',
          data: 'foundDate',
          width: '134px',
          render: (data, type, full, meta) => {
            return unixTimestampFilter(data)
          }
        },
        {
          label: '状态',
          data: 'state',
          render: (data, full) => {
            return data === 1 ? '启用' : '停用'
          }
        },
        {
          label: '默认线路起点',
          data: 'isDefault',
          render: (data, full) => {
            return data ? '是' : '否'
          }
        }
      ]
    }
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    refresh() {
      this.searchFunc(this.search)
    },
    showUpdate() {
      console.log('showUpdate')
      this.$refs.AddWarehouse.show()
    },
    addWarehouse() {
      this.warehouseData = {}
      this.$refs.AddWarehouse.show()
    },
    update(data) {
      if (data) {
        this.warehouseData = data
      }
      this.$refs.AddWarehouse.show()
    },
    details(data) {
      if (data) {
        this.warehouseData = data
      }
      this.$refs.DetailsDialog.show()
    },
    setting(data) {
      this.$router.push({path: '/ss-oms/warehouse/configuration/edit/' + data.id})
    },
    deletes(data) {
      let url = '/ms-warehouse-order/ch-warehouse-manager/deleteWarehouse'
      let content = '<p class="font-red">确定要删除该仓库？</p>'
      this.operate(data, content, url)
    },
    stop(data) {
      let url = '/ms-warehouse-order/ch-warehouse-manager/stopWarehouse'
      let content = '<p class="font-red">确定继续停用仓库？</p><p class="font-red">关闭仓库将同时停用该仓库关联线路</p>'
      this.stopCheckUp(data, content, url)
    },
    start(data) {
      let url = '/ms-warehouse-order/ch-warehouse-manager/startWarehouse'
      let content = '<p class="font-red">确定要启用该仓库？</p>'
      this.operate(data, content, url)
    },
    stopCheckUp(data, content, url) {
      return this.$ajax.get('/ms-warehouse-order/ch-warehouse-manager/checkLineUsring', {warehouseId: data.id}).then(response => {
        if (response.success) {
          if (response.data === 0) {
            content = '<p class="font-red">目前该仓库还有货在仓库中</p><p class="font-red">暂不能停用</p>'
            confirm(content, '警告', {
              dangerouslyUseHTMLString: true,
              showCancelButton: false,
              type: 'error'
            })
          } else {
            this.operate(data, content, url)
          }
        }
      })
    },
    stopOrStart(data) {
      if (data.state === 1) {
        this.stop(data)
      } else {
        this.start(data)
      }
    },
    del(data) {
      alert('调试中')
    },
    operate(data, content, url) {
      confirm(content, '警告', {
        type: 'warning',
        confirmButtonLoading: true,
        dangerouslyUseHTMLString: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          return this.$ajax.get(url, {id: data.id}).then(response => {
            if (response.success) {
              close().then(_ => {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.refresh()
              })
            } else {
              confirmButtonLoadingClose()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
